package sk.naive.talker;

import sk.naive.talker.persistence.*;
import sk.naive.talker.util.Utils;
import sk.naive.talker.props.*;
import sk.naive.talker.server.DbLayer;

import java.util.*;

/**
 * Long message representation - board posts, profiles.
 * <p>
 * Text id have to be greater then 0 and can be set only once.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.16 $ $Date: 2007-05-07 10:34:20 $
 */
public class Text implements PersistentObject {
	public static final String TPROP_CONTENT = "content";
	public static final String TPROP_CREATED = "created";
	public static final String TPROP_MODIFIED = "modified";
	public static final String TPROP_AUTHOR = "author";
	public static final String TPROP_LAST_READ = "lastRead";

	private PersistentPropertyStoreImpl store;

	public Text(TextPersistence tp, String content) throws PersistenceException {
		store = new PersistentPropertyStoreImpl(tp.createNewText(), null);

		set(TPROP_CREATED, Utils.timestamp(), tp);
		setContent(content, tp);
	}

	public Text(TextPersistence tp, String content, Integer author) throws PersistenceException {
		store = new PersistentPropertyStoreImpl(tp.createNewText(), null);

		set(TPROP_CREATED, Utils.timestamp(), tp);
		setContent(content, tp);
		setAuthor(author, tp);
	}

	public Text(Integer id) {
		store = new PersistentPropertyStoreImpl(id, null);
	}

	public String getContent() throws PersistenceException {
		return (String) getFromTextPersistence(TPROP_CONTENT);
	}

	/**
	 * Text modification.
	 * @param content new content
	 */
	public void setContent(String content, TextPersistence tp) throws PersistenceException {
		set(TPROP_CONTENT, content, tp);
		set(TPROP_MODIFIED, Utils.timestamp(), tp);
	}

	public Integer getAuthorId(TextPersistence tp) throws PersistenceException {
		return Integer.parseInt((String) getFromTextPersistence(TPROP_AUTHOR));
	}

	public void setAuthor(Integer author, TextPersistence tp) throws PersistenceException {
		set(TPROP_AUTHOR, String.valueOf(author), tp);
	}

	public void updateLastRead(TextPersistence tp) throws PersistenceException {
		set(TPROP_LAST_READ, Utils.timestamp(), tp);
	}

	public Date getCreated(TextPersistence tp) throws PersistenceException {
		return Utils.timestampToDate((String) getFromTextPersistence(TPROP_CREATED));
	}

	public Date getModified(TextPersistence tp) throws PersistenceException {
		return Utils.timestampToDate((String) getFromTextPersistence(TPROP_MODIFIED));
	}

	public Object getFromTextPersistence(String key) throws PersistenceException {
		return new DbLayer().loadProperty(getId(), key);
	}

	public void set(String key, String value, TextPersistence tp) throws PersistenceException {
		tp.storeProperty(getId(), key, value);
	}

	public Object get(String key) {
		throw new UnsupportedOperationException();
	}

	public void setNonpersistent(String property, Object value) {
		throw new UnsupportedOperationException();
	}

	public void remove() throws PersistenceException {
		new DbLayer().remove(getId());
	}

	public Integer getId() {
		return store.getId();
	}

	public void setStoredValue(String key, String value) throws PropertyStoreException {
		store.setStoredValue(key, value);
	}

	public void set(String property, Object value) throws PropertyStoreException {
		store.set(property, value);
	}

	public Map<String,Object> getProperties() {
		return store.getProperties();
	}

	public Property getProperty(String key) {
		return store.getProperty(key);
	}
}
